home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / filelock.c < prev    next >
C/C++ Source or Header  |  1992-04-26  |  9KB  |  366 lines

  1. /* Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
  2.  
  3. This file is part of GNU Emacs.
  4.  
  5. GNU Emacs is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GNU Emacs is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Emacs; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19.  
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include "config.h"
  23. #ifdef hpux
  24. /* needed by <pwd.h> */
  25. #include <stdio.h>
  26. #undef NULL
  27. #endif
  28. #include "lisp.h"
  29. #include "paths.h"
  30. #include "buffer.h"
  31.  
  32. #ifdef VMS
  33. #include "pwd.h"
  34. #else
  35. #include <pwd.h>
  36. #endif
  37.  
  38. #include <errno.h>
  39. #include <sys/file.h>
  40. #ifdef USG
  41. #include <fcntl.h>
  42. #endif /* USG */
  43.  
  44. extern int errno;
  45.  
  46. #ifdef VMS
  47. /* Prevent the file from being totally empty.  */
  48. static dummy () {}
  49. #endif
  50.  
  51. #ifdef CLASH_DETECTION
  52.   
  53. /* If system does not have symbolic links, it does not have lstat.
  54.    In that case, use ordinary stat instead.  */
  55.  
  56. #ifndef S_IFLNK
  57. #define lstat stat
  58. #endif
  59.  
  60. static Lisp_Object
  61. lock_file_owner_name (lfname)
  62.      char *lfname;
  63. {
  64.   struct stat s;
  65.   struct passwd *the_pw;
  66.   extern struct passwd *getpwuid ();
  67.  
  68.   if (lstat (lfname, &s) == 0)
  69.     the_pw = getpwuid (s.st_uid);
  70.   return (the_pw == 0 ? Qnil : build_string (the_pw->pw_name));
  71. }
  72.  
  73.  
  74. /* lock_file locks file fn,
  75.    meaning it serves notice on the world that you intend to edit that file.
  76.    This should be done only when about to modify a file-visiting
  77.    buffer previously unmodified.
  78.    Do not (normally) call lock_buffer for a buffer already modified,
  79.    as either the file is already locked, or the user has already
  80.    decided to go ahead without locking.
  81.  
  82.    When lock_buffer returns, either the lock is locked for us,
  83.    or the user has said to go ahead without locking.
  84.  
  85.    If the file is locked by someone else, lock_buffer calls
  86.    ask-user-about-lock (a Lisp function) with two arguments,
  87.    the file name and the name of the user who did the locking.
  88.    This function can signal an error, or return t meaning
  89.    take away the lock, or return nil meaning ignore the lock.  */
  90.  
  91. /* The lock file name is the file name with "/" replaced by "!"
  92.    and put in the Emacs lock directory.  */
  93. /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
  94.  
  95. void
  96. lock_file (fn)
  97.      register Lisp_Object fn;
  98. {
  99.   register Lisp_Object attack;
  100.   register char *lfname;
  101.  
  102.   /* Create the name of the lock-file for file fn */
  103.   lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
  104.   fill_in_lock_file_name (lfname, fn);
  105.  
  106.   /* See if this file is visited and has changed on disk since it was visited.  */
  107.   {
  108.     register Lisp_Object subject_buf = Fget_file_buffer (fn);
  109.     if (!NULL (subject_buf)
  110.     && NULL (Fverify_visited_file_modtime (subject_buf))
  111.     && !NULL (Ffile_exists_p (fn)))
  112.       call1 (intern ("ask-user-about-supersession-threat"), fn);
  113.   }
  114.  
  115.   /* Try to lock the lock. */
  116.   if (lock_if_free (lfname) <= 0)
  117.     /* Return now if we have locked it, or if lock dir does not exist */
  118.     return;
  119.  
  120.   /* Else consider breaking the lock */
  121.   attack = call2 (intern ("ask-user-about-lock"), fn,
  122.           lock_file_owner_name (lfname));
  123.   if (!NULL (attack))
  124.     /* User says take the lock */
  125.     {
  126.       lock_superlock (lfname);
  127.       lock_file_1 (lfname, O_WRONLY) ;
  128.       unlink (PATH_SUPERLOCK);
  129.       return;
  130.     }
  131.   /* User says ignore the lock */
  132. }
  133.  
  134. fill_in_lock_file_name (lockfile, fn)
  135.      register char *lockfile;
  136.      register Lisp_Object fn;
  137. {
  138.   register char *p;
  139.  
  140.   strcpy (lockfile, PATH_LOCK);
  141.  
  142.   p = lockfile + strlen (lockfile);
  143.  
  144.   strcpy (p, XSTRING (fn)->data);
  145.  
  146.   for (; *p; p++)
  147.     {
  148.       if (*p == '/')
  149.     *p = '!';
  150.     }
  151. }
  152.  
  153. /* Lock the lock file named LFNAME.
  154.    If MODE is O_WRONLY, we do so even if it is already locked.
  155.    If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
  156.    Return 1 if successful, 0 if not.  */
  157.  
  158. int
  159. lock_file_1 (lfname, mode)
  160.      int mode; char *lfname; 
  161. {
  162.   register int fd;
  163.   char buf[20];
  164.  
  165.   if ((fd = open (lfname, mode, 0666)) >= 0)
  166.     {
  167.       fchmod (fd, 0666);
  168.       sprintf (buf, "%d ", getpid ());
  169.       write (fd, buf, strlen (buf));
  170.       close (fd);
  171.       return 1;
  172.     }
  173.   else
  174.     return 0;
  175. }
  176.  
  177. /* Lock the lock named LFNAME if possible.
  178.    Return 0 in that case.
  179.    Return positive if lock is really locked by someone else.
  180.    Return -1 if cannot lock for any other reason.  */
  181.  
  182. int
  183. lock_if_free (lfname)
  184.      register char *lfname; 
  185. {
  186.   register int clasher;
  187.  
  188.   while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
  189.     {
  190.       if (errno != EEXIST)
  191.     return -1;
  192.       clasher = current_lock_owner (lfname);
  193.       if (clasher != 0)
  194.     if (clasher != getpid ())
  195.       return (clasher);
  196.     else return (0);
  197.       /* Try again to lock it */
  198.     }
  199.   return 0;
  200. }
  201.  
  202. /* Return the pid of the process that claims to own the lock file LFNAME,
  203.    or 0 if nobody does or the lock is obsolete,
  204.    or -1 if something is wrong with the locking mechanism.  */
  205.  
  206. int
  207. current_lock_owner (lfname)
  208.      char *lfname;
  209. {
  210.   int owner = current_lock_owner_1 (lfname);
  211.   if (owner == 0 && errno == ENOENT)
  212.     return (0);
  213.   /* Is it locked by a process that exists?  */
  214.   if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
  215.     return (owner);
  216.   if (unlink (lfname) < 0)
  217.     return (-1);
  218.   return (0);
  219. }
  220.  
  221. int
  222. current_lock_owner_1 (lfname)
  223.      char *lfname;
  224. {
  225.   register int fd;
  226.   char buf[20];
  227.   int tem;
  228.  
  229.   fd = open (lfname, O_RDONLY, 0666);
  230.   if (fd < 0)
  231.     return 0;
  232.   tem = read (fd, buf, sizeof buf);
  233.   close (fd);
  234.   return (tem <= 0 ? 0 : atoi (buf));
  235. }
  236.  
  237.  
  238. void
  239. unlock_file (fn)
  240.      register Lisp_Object fn;
  241. {
  242.   register char *lfname;
  243.  
  244.   lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
  245.   fill_in_lock_file_name (lfname, fn);
  246.  
  247.   lock_superlock (lfname);
  248.  
  249.   if (current_lock_owner_1 (lfname) == getpid ())
  250.     unlink (lfname);
  251.  
  252.   unlink (PATH_SUPERLOCK);
  253. }
  254.  
  255. lock_superlock (lfname)
  256.      char *lfname;
  257. {
  258.   register int i, fd;
  259.  
  260.   for (i = -20; i < 0 && (fd = open (PATH_SUPERLOCK,
  261.                      O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
  262.        i++)
  263.     {
  264.       if (errno != EEXIST)
  265.     return;
  266.       sleep (1);
  267.     }
  268.   if (fd >= 0)
  269.     {
  270.       fchmod (fd, 0666);
  271.       write (fd, lfname, strlen (lfname));
  272.       close (fd);
  273.     }
  274. }
  275.  
  276. void
  277. unlock_all_files ()
  278. {
  279.   register Lisp_Object tail;
  280.   register struct buffer *b;
  281.  
  282.   for (tail = Vbuffer_alist; XGCTYPE (tail) == Lisp_Cons;
  283.        tail = XCONS (tail)->cdr)
  284.     {
  285.       b = XBUFFER (XCONS (XCONS (tail)->car)->cdr);
  286.       if (XTYPE (b->filename) == Lisp_String &&
  287.       b->save_modified < BUF_MODIFF (b))
  288.     unlock_file (b->filename);
  289.     }
  290. }
  291.  
  292.  
  293. DEFUN ("lock-buffer", Flock_buffer, Slock_buffer,
  294.   0, 1, 0,
  295.   "Locks FILE, if current buffer is modified.\n\
  296. FILE defaults to current buffer's visited file,\n\
  297. or else nothing is done if current buffer isn't visiting a file.")
  298.   (fn)
  299.      Lisp_Object fn;
  300. {
  301.   if (NULL (fn))
  302.     fn = current_buffer->filename;
  303.   else
  304.     CHECK_STRING (fn, 0);
  305.   if (current_buffer->save_modified < MODIFF
  306.       && !NULL (fn))
  307.     lock_file (fn);
  308.   return Qnil;    
  309. }
  310.  
  311. DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer,
  312.   0, 0, 0,
  313.  "Unlocks the file visited in the current buffer,\n\
  314. if it should normally be locked.")
  315.   ()
  316. {
  317.   if (current_buffer->save_modified < MODIFF &&
  318.       XTYPE (current_buffer->filename) == Lisp_String)
  319.     unlock_file (current_buffer->filename);
  320.   return Qnil;
  321. }
  322.  
  323.  
  324. /* Unlock the file visited in buffer BUFFER.  */
  325.  
  326. unlock_buffer (buffer)
  327.      struct buffer *buffer;
  328. {
  329.   if (buffer->save_modified < BUF_MODIFF (buffer)
  330.       && XTYPE (buffer->filename) == Lisp_String)
  331.     unlock_file (buffer->filename);
  332. }
  333.  
  334. DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0,
  335.   "Returns nil if the FILENAME is not locked,\n\
  336. t if it is locked by you, else a string of the name of the locker.")
  337.   (fn)
  338.   Lisp_Object fn;
  339. {
  340.   register char *lfname;
  341.   int owner;
  342.  
  343.   fn = Fexpand_file_name (fn, Qnil);
  344.  
  345.   /* Create the name of the lock-file for file filename */
  346.   lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
  347.   fill_in_lock_file_name (lfname, fn);
  348.  
  349.   owner = current_lock_owner (lfname);
  350.   if (owner <= 0)
  351.     return (Qnil);
  352.   else if (owner == getpid ())
  353.     return (Qt);
  354.   
  355.   return (lock_file_owner_name (lfname));
  356. }
  357.  
  358. syms_of_filelock ()
  359. {
  360.   defsubr (&Sunlock_buffer);
  361.   defsubr (&Slock_buffer);
  362.   defsubr (&Sfile_locked_p);
  363. }
  364.  
  365. #endif /* CLASH_DETECTION */
  366.